using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Framework.Library.Components.Security;
using Framework.Library.Components.Users;
using Framework.Library;
using Framework.Library.Components.Portal;
using Framework.Library.Utilities;
using Framework.Library.Components.Commands;
using Framework.Module.CMS.Library.Zones;

namespace Framework.Module.CMS.Admin.Zones
{
    public partial class ZonePermision : AuthenticatedControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblUpdateStatus.Text = string.Empty;
            if (!IsPostBack)
            {
                DataTable dtUsers;                
                dtUsers = UserController.GetUsers("", "", "", ConvertUtility.ToInt32(AppEnv.PortalId()));
                
                dtgUsers.DataSource = dtUsers;
                dtgUsers.DataBind();

                //Fill portals
                dropPortal.Items.Clear();                
                PortalController.LoadPortals(dropPortal.Items);
                dropPortal.SelectedValue = AppEnv.PortalId();

                string cmd = ConvertUtility.ToString(Request.QueryString["cmd"]);
                if (cmd != null && cmd != "")
                {
                    CommandInfo info = CommandController.GetCommand(cmd);
                    lblCommandName.Text = info.CommandName;
                }
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            LoadZones();
            ZonePermissionInfo curPermision = ZonePermissionController.GetPermision(ConvertUtility.ToInt32(txtUserID.Text), ConvertUtility.ToInt32(dropPortal.SelectedValue));
            rdoPermisions.SelectedIndex = -1;
            if (curPermision == null) return;

            MiscUtility.SelectItemFromList(rdoPermisions, curPermision.Permision.ToString());

            foreach (ListItem item in lstZones.Items)
                if (curPermision.Zones.IndexOf("|" + item.Value + "|") > -1) item.Selected = true;
                else item.Selected = false;

        }

        protected void LoadZones()
        {
            lstZones.Items.Clear();
            ZoneController.LoadZones(lstZones.Items, dropPortal.SelectedValue);
            lstZones.Items.Insert(0, new ListItem("Root", "0"));
        }

        protected void dtgUsers_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "roles")
            {
                int userId = ConvertUtility.ToInt32(e.CommandArgument);
                UserInfo user = UserController.GetUser(userId);
                if (user == null) return;
                txtUserID.Text = user.UserID.ToString();
                lblUsername.Text = user.Username;

            }
        }
        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            if (lblUsername.Text == string.Empty || txtUserID.Text == string.Empty) return;
            try
            {
                int userId = ConvertUtility.ToInt32(txtUserID.Text);
                int permision = ConvertUtility.ToInt32(rdoPermisions.SelectedValue, -1);
                if (permision == -1)
                {
                    ZonePermissionController.RemoveUserPermision(userId, ConvertUtility.ToInt32(dropPortal.SelectedValue));
                    return;
                }
                string permisionZoneCollection = "|";
                foreach (ListItem item in lstZones.Items)
                {
                    if (item.Selected) permisionZoneCollection += item.Value + "|";
                }
                if (permisionZoneCollection.Length == 1) permisionZoneCollection = string.Empty;
                ZonePermissionController.UpdateUserPermision(userId, permision, permisionZoneCollection, ConvertUtility.ToInt32(dropPortal.SelectedValue));
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = ex.Message;
            }
        }
        protected void cmdCancel_Click(object sender, EventArgs e)
        {
            rdoPermisions.SelectedIndex = -1;
            lstZones.SelectedIndex = -1;
        }
    }
}